<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/2
 * Time: 15:42
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class Bargain extends Common
{
    public function goods()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq', 0);
            if (isset($pagedata['shop_name']) && $pagedata['shop_name'] != '') {
                $where['goods_name'] = array('like', '%' . $pagedata['shop_name'] . '%');
            }

            $count = Db::name('bargain_goods')->where($where)->count();
            $data = Db::name('bargain_goods')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {

            return view();
        }
    }

    /*
     * 添加商品
     */
    public function addGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传商品缩略图'];
                exit;
            }

            if (!$post['content']) {
                return ['code' => 1, 'msg' => '商品详情不能为空'];
                exit;
            }

            $data = [
                'goods_name' => $post['goods_name'],
                'goods_money' => $post['goods_money'],
                'goods_image' => $post['article_thumb'],
                'goods_content' => $post['content'],
                'goods_number' => $post['goods_number'],
                'bargain_money' => $post['bargain_money'],
                'join_count' => $post['join_count'],
                'create_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::name('bargain_goods')->insert($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {

            return $this->fetch();
        }
    }

    /*
     * 是否上加
     */
    public function isShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
             //查看当前产品是否已经有正在砍价的
            $result = Db::name('bargain_start')
            ->where(['goods_id'=>$post['id'],'is_have'=>0])
            ->select();
            if(count($result)>0){
                 return ['code' => 1, 'msg' => '用户正在砍价,请勿轻易修改'];
            }
            $res = Db::name('bargain_goods')->where('id', $post['id'])->setField('is_display', $post['is_display']);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 删除
     */
    public function del()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('bargain_goods')->where('id', $post['id'])->setField('is_del', 1);
            if ($res) {
                return '1';
            } else {
                return '2';
            }
        }
    }

    /*
     * 修改
     */
    public function editGoods()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            //查看当前产品是否已经有正在砍价的
            $result = Db::name('bargain_start')
            ->where(['goods_id'=>$post['id'],'is_have'=>0])
            ->select();
            if(count($result)>0){
                 return ['code' => 1, 'msg' => '用户正在砍价,请勿轻易修改'];
            }


            unset($post['file']);
            if (!$post['article_thumb']) {
                return ['code' => 1, 'msg' => '请上传商品缩略图'];
                exit;
            }

            if (!$post['content']) {
                return ['code' => 1, 'msg' => '商品详情不能为空'];
                exit;
            }

            $data = [

                'goods_name' => $post['goods_name'],
                'goods_money' => $post['goods_money'],
                'goods_image' => $post['article_thumb'],
                'goods_content' => $post['content'],
                'bargain_money' => $post['bargain_money'],
                'goods_number' => $post['goods_number'],
                'join_count' => $post['join_count'],

            ];
            $res = Db::name('bargain_goods')->where('id', $post['id'])->update($data);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        } else {
            $id = Request::instance()->get();
            $data = Db::name('bargain_goods')->where('id', $id['id'])->find();

            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /*
   * 商品详情
   */
    public function details()
    {
        $id = input('id', '', 'trim');
        $content = Db::name('bargain_goods')->field('goods_content as content')->where('id', $id)->find();
        $this->assign('content', $content);
        return $this->fetch();
    }


    /*********************************************发起砍价*********************************/
    public function bargain()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['shop_name']) && $pagedata['shop_name'] != '') {
                $where['goods_name'] = array('like', '%' . $pagedata['shop_name'] . '%');
            }
            if (isset($pagedata['nickname']) && $pagedata['nickname'] != '') {
                $where['nickname'] = array('like', '%' . $pagedata['nickname'] . '%');
            }
             if (isset($pagedata['is_have']) && $pagedata['is_have'] != '') {
                $where['is_have'] =  $pagedata['is_have'];
            }
            $count = Db::name('bargain_start')->field('a.*,g.goods_name,u.nickname')->alias('a')
                ->join('bargain_goods g', 'a.goods_id=g.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')->where($where)->count();
            $data = Db::name('bargain_start')->field('a.*,g.goods_name,u.nickname')->alias('a')
                ->join('bargain_goods g', 'a.goods_id=g.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {

            return view();
        }
    }

    public function bargain_list()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('bargain_list')->alias('a')->field('a.bargain_money,a.create_time,u.nickname')
                ->join('user u', 'a.help_id=u.uid', 'left')
                ->where('start_id', $pagedata['id'])->count();
            $res = Db::name('bargain_list')->alias('a')->field('a.bargain_money,a.create_time,u.nickname')
                ->join('user u', 'a.help_id=u.uid', 'left')
                ->where('start_id', $pagedata['id'])
                ->limit($startrow, $pagesize)
                ->order('a.id', 'desc')
                ->select();
            if ($res) {
                return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $res));
            } else {
                return json(array('code' => 0, 'msg' => '请求成功', 'count' => 0, 'data' => $res));
            }
        }
    }

    public function order()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where = [];
            if (isset($pagedata['order_sn']) && $pagedata['order_sn'] != '') {
                $where['order_sn'] = array('like', '%' . $pagedata['order_sn'] . '%');
            }
            if (isset($pagedata['order_status']) && $pagedata['order_status'] != '') {
                $where['order_status'] = array('like', '%' . $pagedata['order_status'] . '%');
            }
          if (isset($pagedata['create_time']) && $pagedata['create_time'] != ''){
                $created_at=explode(' - ', $pagedata['create_time']); 
                if (isset($created_at) && is_array($created_at)){
                    foreach($created_at as $k=>$v)
                    {
                       $created_at[$k] = strtotime($v);
                    }  
                } 
              $where['create_time']=['between time',$created_at];      
            } 
              
            $count = Db::name('bargain_order')
                ->where($where)->count();
            $data = Db::name('bargain_order')
                ->where($where)
                ->limit($startrow, $pagesize)
                ->order('order_id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('bargain_order')->field('order_sn')->select();
            $this->assign('res', $res);
            return view();
        }
    }

    public function send_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'dk_code' => $post['data']['content'],
                'is_send' => 1
            ];
            $res = Db::name('bargain_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                //查询 发送站内消息
                $user = Db::name('bargain_order')->alias('a')->field('a.dk_code,a.uid,b.goods_name')->join('bargain_goods b', 'a.goods_id=b.id', 'left')
                    ->where('order_id', $post['data']['id'])
                    ->find();
                send_user_mes($user['uid'], "发货通知", "您在砍价中购买的" . $user['goods_name'] . "已发货,发货单号为" . $user['dk_code'] . "请注意查收");
                return ['code' => 0, 'data' => $user];
            } else {
                return ['code' => 1];
            }
        }
    }

    public function edit_code()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'dk_code' => $post['data']['code'],
            ];
            $res = Db::name('bargain_order')->where('order_id', $post['data']['id'])->update($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }


    /***************************************规则*******************************************/
    public function task()
    {
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $count = Db::name('bargain_rule')->count();
            $data = Db::name('bargain_rule')
                ->limit($startrow, $pagesize)
                ->order('id', 'desc')
                ->select();
            return json(array('code' => 0, 'msg' => '请求成功', 'count' => $count, 'data' => $data));
        } else {
            $res = Db::name('bargain_rule')->select();
            $this->assign('res', $res);
            return view();
        }
    }

    public function addRule()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $data = [
                'content' => $post['data']['content'],
            ];
            $res = Db::name('bargain_rule')->insert($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    public function editTask()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            $data = [
                'content'=>$post['content']
            ];
            $res = Db::name('bargain_rule')->where('id',$post['id'])->update($data);

            if ($res !== false) {
                return ['code' => 1];
            } else {
                return ['code' => 0];
            }
        }else{
            $id = $_GET['id'];
            //查询数据库
            $data = Db::name('bargain_rule')
                ->where('id', $id)
                ->find();
            if ($data !== false) {
                $this->assign('data', $data);
            }
            return $this->fetch();
        }
    }

}